package com.hubstc.book_borrow.controller;

import com.hubstc.book_borrow.pojo.Result;
import com.hubstc.book_borrow.service.BorrowService;
import com.hubstc.book_borrow.utils.JwtUtil;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/borrow")
public class BorrowController {

    @Autowired
    private BorrowService borrowService;

    @PostMapping("/book")
    public Result addBorrow(String readerId,String bookId){
        borrowService.addBorrow(readerId,bookId);
        return Result.success();
    }

    @PostMapping("/return")
    public Result returnBorrow(Integer borrowId){
        borrowService.returnBorrow(borrowId);
        return Result.success();
    }

    @PostMapping("/record")
    public Result selectBorrow(@RequestHeader(name="token")String token){
        Map<String,Object> claims= JwtUtil.parseToken(token);
        String readerId=(String) claims.get("readerId");
        return Result.success(borrowService.selectBorrow(readerId));
    }
}
